package com.lkm.market2_0.m_imple.steelyardserver.server.cmd_handle;

import com.lkm.frame.P;
import com.lkm.javatool.NumberHelp;
import com.lkm.javatool.StringHelp;
import com.lkm.javatool.lang.RunnablePR;
import com.lkm.market2_0.ExpandConfig;
import com.lkm.market2_0.entity.SteelyardInfo;
import com.lkm.market2_0.help.DataEncryptionHelp;
import com.lkm.market2_0.help.UIP;
import com.lkm.market2_0.m.dataUp.DataUpFace;
import com.lkm.market2_0.m.steelyarddata.SteelyardDataModule;
import com.lkm.market2_0.m.steelyardserver.SteelyardServerModule;
import com.lkm.market2_0.m_imple.steelyardserver.server.CMDEntity;
import com.lkm.market2_0.m_imple.steelyardserver.server.ICMDHandle;
import com.lkm.market2_0.m_imple.steelyardserver.server.AbsReceiverDataHandle;

import java.util.regex.Pattern;




public class CMDHeartbeatHandle implements ICMDHandle {

    public static void main(String[] args) {
        try {


            System.out.println(new String(new byte[]{39, 16, 0, 32, 72, 75, 0, 0, 117, 48, 0, 96}, "gbk"));
            String str = "1234567890Hs";

            System.out.println(new String(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, "gbk"));

            Pattern pattern =  Pattern.compile("[0-9a-zA-z]{12}");
            pattern.matcher(str).matches();






//            System.out.println(str.matches());//("太1234567890".getBytes("gbk").length);
//            boolean flag = java.nio.charset.Charset.forName("ISO-8859-1").newEncoder().canEncode(str);

//            System.out.println(flag);
        }catch (Exception e){}


        System.out.println((byte)0xff);

        byte[] s = new byte[]{0x42};
        System.out.println(s[0]/10f);



        System.out.println(NumberHelp.bytesTohexDesc(new byte[]{0x31, 0x36}));
    }

    SteelyardDataModule mSteelyardDataModule;
    SteelyardServerModule mSteelyardServerModule;

    public CMDHeartbeatHandle() {
        mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);
        mSteelyardServerModule= ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
    }
    @Override
    public AbsReceiverDataHandle handle(CMDEntity cmdEntity) {
        return handle(cmdEntity,null);
    }

    public AbsReceiverDataHandle handle(CMDEntity cmdEntity, RunnablePR<String ,Void> onSteelyardMac) {

        if(mSteelyardDataModule==null)
        mSteelyardDataModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardDataModule.class);

//        if(P.isDebug())
//        P.p("======CMD称重心跳"+cmdEntity.getSteelyardMac());

        byte[] data=cmdEntity.getData();

        
        String  contentHex=NumberHelp.bytesTohexDesc( DataEncryptionHelp.Decrypt(data,cmdEntity.getNum(),24));


        String version = "";
        float voltage = 0;

        
        if (data[24] == 0x31) {
            
            version = NumberHelp.bytesTohexDesc(new byte[]{data[24], data[25]});
        } else {
            
            version = "31"+data[24];
            voltage = (data[25])/10f;
        }





        
        String steelyardNumno = ""+NumberHelp.bytesToLongDesc(new byte[]{data[26], data[27],data[28],data[29]});


        boolean isExceptions = false;
        
        if(contentHex.equals("3377763f124eb67a87c9add4")){
            isExceptions = true;
        } else {
            
            String steelyardMac=contentHex.substring(12,12+12);
            cmdEntity.setSteelyardMac(steelyardMac);

        }

        
        
        if("ffff".equals(NumberHelp.bytesTohexDesc(new byte[]{data[24], data[25]}))){
            isExceptions = true;
        }
//        if("31-1".equals(NumberHelp.bytesTohexDesc(new byte[]{data[24], data[25]}))){



        if(ExpandConfig.instance.isUI()){

            
            String steelyardMac = ExpandConfig.instance.getServerSteelyardMac(steelyardNumno, cmdEntity.getSteelyardMac());
            cmdEntity.setSteelyardMac(steelyardMac);

            if (StringHelp.isEmpty(steelyardMac)) {
                return null;
            }

        }
        if (onSteelyardMac != null) {
            onSteelyardMac.run(cmdEntity.getSteelyardMac());
        }


        if(isExceptions){
            
            if(ExpandConfig.instance.isUI()){
                try {
                    if (mSteelyardServerModule == null)
                        mSteelyardServerModule = ExpandConfig.instance.getModuleManager().loadModule(SteelyardServerModule.class);
                    mSteelyardServerModule.setSteelyardOK(cmdEntity.getSteelyardMac());
                }catch (Exception e){}
            }

        }

        if(isExceptions){
            mSteelyardDataModule.steelyardHeartbeatCollect(cmdEntity.getSteelyardMac(),System.currentTimeMillis(),true,version,steelyardNumno, voltage);
        } else {
            mSteelyardDataModule.steelyardHeartbeatCollect(cmdEntity.getSteelyardMac(),System.currentTimeMillis(),!true,version,steelyardNumno,voltage);
        }

        if(P.isDebug()) {
            String log="CMD心跳："+( contentHex.equals("3377763f124eb67a87c9add4")?"异常":"正常" )+contentHex+"  version： "+version+"  名牌信息："+steelyardNumno;

            P.p("====="+log);
            if(UIP.isPrint()){
                UIP.p(log,cmdEntity.getSteelyardMac());
            }
        }

        if(!StringHelp.isEmpty(cmdEntity.getSteelyardMac())) {
            mSteelyardServerModule.tickOK_block(cmdEntity.getSteelyardMac());


            SteelyardInfo steelyardInfo = mSteelyardDataModule.getSteelyardInfo(cmdEntity.getSteelyardMac());

            if(steelyardInfo!=null) {
//                OrganizationInfo organizationInfo = AppDataCache.getInstance().getOrganizationEntity(steelyardInfo.getOrg());
                DataUpFace.updateStatus(steelyardInfo,isExceptions);
            }


        }



        return null;
    }




}
